package com.share.tool.generator.maven;

import com.share.common.utils.FileUtil;
import com.share.common.utils.JarFileUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 生成maven部署的命令
 */
public class MainGenDeploy {
    public static String GROUP_ID = "unknow.com";
    public static String PACKAGING = "jar";
    public static String NEXUS = " -Durl=http://nexus.snyq.ltd:8081/repository/maven-third/ -DrepositoryId=nexus.snyq.ltd";

    public static void main(String[] args) {
        genAll();
    }

    public static void genAll() {
//        String dir = "C:\\Users\\Administrator\\Downloads\\TechSDKDemoForJava\\TechSDKDemoForJava\\libs\\";
        String dir = "D:\\project\\we-share\\share-admin\\target\\share-admin\\BOOT-INF\\lib";
        List<File> files = FileUtil.listCurrentDirFile(dir, null, null);
        StringBuffer sb1 = new StringBuffer();
        StringBuffer sb2 = new StringBuffer();
        for (File file : files) {
            String name = file.getName();
            String arr[] = calcMaven(name);

            String mainPackage = JarFileUtil.getMainPackage(file.getAbsolutePath());
            mainPackage = mainPackage == null ? GROUP_ID : mainPackage;
            String dep = genDependency(mainPackage, arr[0], arr[1]);
            String mvn = genMvnDeploy(mainPackage, arr[0], arr[1], file.getAbsolutePath(), PACKAGING);
            sb1.append(dep).append(System.lineSeparator());
            sb2.append(mvn).append(System.lineSeparator());
        }
        System.out.println(sb1.toString());
        System.out.println(sb2.toString());
    }

    //分离jar的文件名字,分离出坐标
    private static String[] calcMaven(String name) {
        String regVersion = null, artifactId = null, version = null;
        name = name.substring(0, name.length() - 4); //剔除.jar

        //第一种规则
        String regex1 = "-[0-9]+.*"; //*-数字任意字符
        Pattern p = Pattern.compile(regex1);
        Matcher m = p.matcher(name);
        while (m.find()) {
            regVersion = m.group();
            break;
        }
        if (StringUtils.isNotBlank(regVersion) && regVersion.length() > 1) {
            version = regVersion.substring(1, regVersion.length());
            artifactId = name.substring(0, name.indexOf(regVersion));
        }

        return new String[]{artifactId, version};
    }

    public static String genDependency(String groupId, String artifactId, String version) {
        String str = "<dependency>\n" +
                "   <groupId>#groupId#</groupId>\n" +
                "   <artifactId>#artifactId#</artifactId>\n" +
                "   <version>#version#</version>\n" +
                "</dependency>";
        return str.replaceAll("#groupId#", groupId).replaceAll("#artifactId#", artifactId).replaceAll("#version#", version);
    }

    public static String genMvnDeploy(String groupId, String artifactId, String version, String jarPath, String packaging) {
        String str = "mvn deploy:deploy-file -Dfile=#jarPath# -DgroupId=#groupId# -DartifactId=#artifactId# -Dversion=#version# -Dpackaging=#packaging#";
        str = str.replaceAll("#groupId#", groupId).replaceAll("#artifactId#", artifactId).replaceAll("#version#", version);
        str = str.replaceAll("#packaging#", packaging);
        str = str.replace("#jarPath#", jarPath);//存在路径 只能使用replace替换否则会有问题
        return str + NEXUS;
    }
}
